<template>
  <div class="table-box">
    <ProTable ref="proTable" title="登录日志" :columns="columns" :request-api="getNLogRecordsList">
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="Delete" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button type="primary" link :icon="View" @click="deleteAccount(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <LogRecordsDialog ref="logRecordsDialogRef" />
  </div>
</template>

<script setup lang="tsx" name="logRecords">
import { ref } from "vue";
import { System } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import LogRecordsDialog from "@/views/system/components/LogRecordsDialog.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { View, Delete } from "@element-plus/icons-vue";
import { getNLogRecordsList, deleteNLogRecords } from "@/api/modules/system";

const proTable = ref<ProTableInstance>();

// 表格配置项
const columns: ColumnProps<System.ResNLogRecordsList>[] = [
  { prop: "id", label: "序列", width: 80 },
  { sortable: true, prop: "logDate", label: "时间", minWidth: 164, align: "left" },
  { sortable: true, prop: "logLevel", label: "级别", minWidth: 100, align: "left" },
  { sortable: true, prop: "logTitle", label: "标题", minWidth: 240, align: "left", search: { el: "input" } },
  {
    sortable: true,
    prop: "logType",
    label: "类型",
    minWidth: 120,
    align: "left",
    search: {
      el: "date-picker",
      key: "createTime",
      label: "时间范围",
      props: {
        type: "datetimerange",
        format: "YYYY-MM-DD",
        valueFormat: "YYYY-MM-DD"
      }
    }
  },
  { sortable: true, prop: "message", label: "消息", minWidth: 120, align: "left" },
  { sortable: true, prop: "machineName", label: "名称", minWidth: 240, align: "left" },
  { sortable: true, prop: "machineIp", label: "ip", minWidth: 100, align: "left" },
  { prop: "operation", label: "操作", fixed: "right", width: 150 }
];

// 删除
const deleteAccount = async (params: System.ResNLogRecordsList) => {
  await useHandleData(deleteNLogRecords, { id: params.id }, `删除【${params.logTitle}】日志`);
  proTable.value?.getTableList();
};

// 打开 dialog(新增、查看、编辑)
const logRecordsDialogRef = ref<InstanceType<typeof LogRecordsDialog> | null>(null);
const openDialog = (title: string, row: Partial<System.ResNLogRecordsList> = {}) => {
  const params = {
    title,
    isView: true,
    row: { ...row }
  };
  logRecordsDialogRef.value?.acceptParams(params);
};
</script>
